package cn.kgc.vue.controller;

import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Role;
import cn.kgc.vue.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 课工场
 * @date 2024/4/11
 * @description
 */
@RestController
@RequestMapping("role")
@Api(tags = "角色模块")
public class RoleController {

    @Resource
    private RoleService roleService;

    @GetMapping("list")
    @ApiOperation("角色列表")
    public ResponseResult roleList(){
        List<Role> list = roleService.list();
        return ResponseResult.success().data("roleList",list);
    }

    @PostMapping("page")
    @ApiOperation("角色分页")
    public ResponseResult rolePage(@RequestBody QueryCondition<Role> queryCondition){
        return roleService.rolePage(queryCondition);
    }


    @PostMapping("addOrUpdate")
    @ApiOperation("添加/修改角色")
    public ResponseResult addOrUpdateRole(@RequestBody Role role){

        // 判定角色名称是否存在
        String roleCh = role.getRoleCh();
        LambdaQueryWrapper<Role> lambda = new QueryWrapper<Role>().lambda();
        lambda.eq(Role::getRoleCh,roleCh);
        Integer roleId = role.getRoleId();


        Role one = roleService.getOne(lambda);
        if (ObjectUtils.isNotNull(one)){
            Integer rid = one.getRoleId();
            if (rid.equals(roleId)){
                roleService.saveOrUpdate(role);
                return ResponseResult.success();
            }
            return  ResponseResult.fail().message("角色名已存在");
        }

        roleService.saveOrUpdate(role);

        return ResponseResult.success();
    }


    @DeleteMapping("delete/{id}")
    @ApiOperation("删除角色")
    @ApiImplicitParam(name = "id",value = "角色id",paramType ="path" ,required = true)
    public ResponseResult deleteRole(@PathVariable("id") Integer id){

        return  roleService.delete(id);
    }

    @PostMapping("addRoleAuth")
    @ApiOperation("分配角色权限")
    public ResponseResult addRoleAuth(@RequestBody Map<String,Object> roleAuth){
        return roleService.addRoleAuth(roleAuth);
    }


    @GetMapping("getById/{id}")
    @ApiOperation("获取角色信息")
    @ApiImplicitParam(name = "id",value = "角色id",paramType ="path" ,required = true)
    public ResponseResult getRoleById(@PathVariable("id") Integer id){
        Role role = roleService.getById(id);

        return ResponseResult.success().data("role",role);
    }

    @GetMapping("roleAuth/{id}")
    @ApiOperation("获取角色权限")
    @ApiImplicitParam(name = "id",value = "角色id",paramType ="path" ,required = true)
    public ResponseResult roleAuth(@PathVariable("id") Integer id){


        return roleService.roleAuth(id);
    }

}
